﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeGenerationPipeline;
using CodeGenerationPipeline.Contracts;
using System.IO;
using VisualStudioProvider.Templates;
using VisualStudioProvider;
using AbstraX;
using Microsoft.VisualStudio.TextTemplating;
using AbstraX.Contracts;

namespace XmlSchemas
{
    public class XmlSchemasGenerator : BaseGenerator
    {
        public override event WatchProcess OnWatchProcess;

        public override string Name
        {
            get
            {
                return "Xml Services Generator";
            }
        }

        public override string Description
        {
            get
            {
                return "";
            }
        }

        public override ICodeGenerationPackage GenerateFrom(Dictionary<string, IElementBuild> builds, DirectoryInfo workspaceDirectory)
        {
            try
            {
                var template = TemplateProvider.ProjectTemplates[@"\XMLManager\XMLManager"];

                var parms = new VSTemplateParameters
                {
                    ProjectName = "CodeGenerationPipelineInterface",
                    FrameworkVersion = "4.0",
                    RegisteredOrganization = "Mind Chemistry Inc.",
                    CopyrightYear = "2012"
                };

                template.CopyAndProcess(@"C:\Users\Ken\Desktop\ClassLibrary\TestProcess", parms);

                var project = new VSProject(@"C:\Users\Ken\Desktop\ClassLibrary\TestProcess\XMLManager.csproj");

                var codeFile = new FileInfo(Path.Combine(workspaceDirectory.FullName, "InterfaceDocument.cs"));

                templateEngineHost.DebugCallback += (sender, e2) =>
                {

                };

                templateEngineHost.GenerateFile<DocumentClassGenerator>(codeFile, "CodeGenerationPipelineInterface", builds.Values.Where(e => e.IsRootObject).ToList());

                project.AddCompileFile(codeFile.FullName, @"Elements\" + codeFile.Name);

                builds.Values.ToList<IElementBuild>().ForEach(e =>
                {
                    codeFile = new FileInfo(Path.Combine(workspaceDirectory.FullName, e.Element.Name + ".cs"));

                    templateEngineHost.GenerateFile<ElementClassGenerator>(e, codeFile, "CodeGenerationPipelineInterface");

                    project.AddCompileFile(codeFile.FullName, @"Elements\" + codeFile.Name);

                    e.Element.Attributes.SelectMany(a => a.Facets).Where(f => f.FacetType.Name == "RestrictionEnumerationAttribute").ToList().ForEach(f =>
                    {
                        var enumCodeFile = new FileInfo(Path.Combine(workspaceDirectory.FullName, e.Element.Name + "Enum.cs"));

                        templateEngineHost.GenerateFile<EnumGenerator>(enumCodeFile, "CodeGenerationPipelineInterface", f.Arguments.Select(a => a.Value.RemoveQuotes()).ToList<string>());

                        project.AddCompileFile(enumCodeFile.FullName, @"Enumerations\" + enumCodeFile.Name);
                    });

                });

                project.Save();

            }
            catch (Exception ex)
            {
                throw;
            }

            return null;
        }

        public override ICodeGenerationPackage GenerateFromPackage(ICodeGenerationPackage incomingPackage)
        {
            throw new NotImplementedException();
        }
    }
}
